﻿using System;

namespace Moqikaka.DAL
{
    using MySql.Data.MySqlClient;
    using System.Data;

    /// <summary>
    /// 登录信息数据
    /// </summary>
    public class LoginLogDAL : GameBaseDAL
    {

        #region  属性
        static string _GetLastLoginTime = @"SELECT  userid,PartnerId,playerid,LoginTime FROM loginlog 
WHERE userid=@UserId AND PartnerId=@PartnerId 
ORDER BY LoginTime DESC LIMIT 1,1";

        static string _GetPlayerRecentlyLoginTime = @"SELECT  userid,PartnerId,playerid,LoginTime FROM loginlog 
WHERE userid=@UserId AND PartnerId=@PartnerId  and LoginTime<@deadline
ORDER BY LoginTime DESC LIMIT 0,2"; 

         static string _insertLoginLogCommand = @"insert into LoginLog (userid,PartnerId,playerid,LoginTime) values
            (@UserId,@partnerId,@playerid,@LoginTime)";

        

        #endregion

        #region 函数

        /// <summary>
        /// 获取某个玩家的最近登录时间
        /// </summary>
        /// <param name="partnerId"></param>
        /// <param name="userId">userid</param>
        /// <param name="deadline">截止日期，此日期之前的</param>
        /// <returns>登录对象</returns>
        public static DataTable GetPlayerRecentlyLoginTime(string userId, Int32 partnerId, DateTime deadline)
        {

            MySqlParameter[] paramList = new MySqlParameter[]
			{
				new MySqlParameter("@UserId", userId),
				new MySqlParameter("@PartnerId", partnerId),
				new MySqlParameter("@deadline", deadline)
			};

            return ExecuteDataTable(GameBaseDAL.DBConnectionString, _GetPlayerRecentlyLoginTime, paramList);
        }

        /// <summary>
        /// 获取上次登录时间 本次除外
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="partnerId"></param>
        /// <returns></returns>
        public static DataTable GetLastLog(string userId, Int32 partnerId)
        {

            MySqlParameter[] paramList = new MySqlParameter[]
			{
				new MySqlParameter("@UserId", userId),
				new MySqlParameter("@PartnerId", partnerId)
			};

            return ExecuteDataTable(GameBaseDAL.DBConnectionString, _GetLastLoginTime, paramList);
        }

        /// <summary>
        /// 插入一条登录日志
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="partnerId"></param>
        /// <param name="loginTime"></param>
        /// <returns></returns>
        public static bool InsertLoginLog(string userId, Int32 partnerId,Guid playerid,DateTime loginTime)
        {

            MySqlParameter[] paramList = new MySqlParameter[]
			{
				new MySqlParameter("@UserId", userId),
				new MySqlParameter("@PartnerId", partnerId),
				new MySqlParameter("@playerid", playerid),
				new MySqlParameter("@loginTime", loginTime),
			};

            return ExecuteNonQuery(GameBaseDAL.DBConnectionString, _insertLoginLogCommand, paramList)>0;
        } 

        #endregion
    }
}
